﻿import mx.data.types.Str;

class Debug {
	private static var output:Boolean;
	public static function get Output():Boolean {
		return output;
	}
	public static function set Output(op:Boolean) {
		output = op;
	}
	private static function Say(o) {
		var msg:String="DBG:: "+o
		if (say != undefined) {
			say(msg);
			return;
		}
		if(output || output==undefined){
			trace(msg);
		}
	}
	public static var say:Function;
	//变量
	//静态函数
	public static function traceAllObj(obj:Object, all:Boolean, n:Number, list:Array) {
		var a:Array = traceAllObj1(obj, all, n, list);
		
		for (var i = 0; i<a.length; i++) {
			//trace("清理:"+a[i]+GetChildf(a[i]));
			a[i]._____traceFlag_____=null;
			a[i]._____traceFlag_____=undefined;
			delete a[i]._____traceFlag_____;
		}
	}
	public static function traceAll() {
		
		for (var i = 0; i < arguments.length; i++) {
			Say(arguments[i] );
		}
	}
	public static function trace(obj:Object):String {
		var rs:String = "";
		var temp:String = "";
		
		temp = "-- 以下Object[" + obj + "]内部件 --";
		Say(temp);
		rs += temp + "\r";
		
		for(var i in obj){
			switch (typeof (obj[i])) {
				case "number" :
					temp = "数字：[" + i + "]=" + obj[i];
					Say(temp);
					rs += temp + "\r";
					break;
				case "string" :
					temp = "字符串：[" + i + "]=" + obj[i];
					Say(temp);
					rs += temp + "\r";
					break;
				case "function" :
					temp = "函数：[" + i + "]=" + obj[i];
					Say(temp);
					rs += temp + "\r";
					break;
				case "boolean" :
					temp = "布尔：[" + i + "]=" + obj[i];
					Say(temp);
					rs += temp + "\r";
					break;
				case "movieclip" :
					temp = "影片剪辑[" + i + "]：" + obj[i];
					Say(temp);
					rs += temp + "\r";
					break;
				case "object" :
					if(obj[i].length>=0 && typeof(obj[i].push)=="function"){
						temp = "数组[" + i + "]：" + obj[i];
						Say(temp);
						rs += temp + "\r";
					}else{
						temp = "OBJECT[" + i + "]：" + obj[i];
						Say(temp);
						rs += temp + "\r";
					}
					break;
				default :
					temp = "未知类型 " + typeof (obj[i]) + ":" + i + " [" + obj[i] + "]";
					Say(temp);
					rs += temp + "\r";
					break;
				}
				//end switch
		}
		temp = "-- ========================= --";
		Say(temp);
		rs += temp + "\r";
		return rs;
	}
	private static function traceAllObj1(obj:Object, all:Boolean, n:Number, list:Array) {
		if (n == undefined) {
			n = 0;
			Say("-- 以下Object["+obj+"]内部件 --");
			list = new Array();
		}
		//list.push(obj);     
		var kg:String = "├";
		for (var i = 0; i<n; i++) {
			kg = " "+kg;
		}
		if (all) {
			//obj.ASSetPropFlags(obj, null, 0, true);
		}
		//trace(typeof(obj._____traceFlag_____)+"=="+obj._____traceFlag_____+"||"+(obj._____traceFlag_____ == undefined));
		if (obj._____traceFlag_____ != true || obj._____traceFlag_____ == undefined) {
			if(typeof(obj._____traceFlag_____)!="object" && typeof(obj._____traceFlag_____)!="moveclip"){
				obj._____traceFlag_____ = true;
			}
			list.push(obj);
			for (var i in obj) {
				
				//trace("现在这个是 "+i+"["+typeof(obj[i])+"] ="+obj[i]+"  | "+obj[i]._____traceFlag_____);
				switch (typeof (obj[i])) {
				case "number" :
					Say(kg+"数字："+i+"="+obj[i]);
					break;
				case "string" :
					Say(kg+"字符串："+i+"="+obj[i]);
					break;
				case "function" :
					Say(kg+"函数："+i+"="+obj[i]);
					break;
				case "boolean" :
					if (i == "_____traceFlag_____") {
						break;
					}
					Say(kg+"布尔："+i+"="+obj[i]);
					break;
				case "movieclip" :
					Say(kg+"影片剪辑["+i+"]："+obj[i]);
					var b = false;
					if (obj[i]._____traceFlag_____ != true || obj[i]._____traceFlag_____ ==undefined) {
						Say(" "+kg+"-- 以下["+i+"]内部部件 --");
						Debug.traceAllObj1(obj[i], all, n+1,list);
						list.push(obj[i]);
					}else{
						Say(" "+kg+"：内部部件略["+GetChildf(obj[i])+"]");	
					}
					break;
				case "array" :
					Say(kg+"数组["+i+"]：");
					var b = false;
					for (var nn in list) {
						if (obj[i] == list[nn]) {
							b = true;
							break;
						}
					}
					if (!b) {
						Say(" "+kg+"-- 以下["+i+"]内部部件 --");
						Debug.traceAllObj1(obj[i], all, n+1,list);
					}else{
						Say(" "+kg+"：内部部件略["+GetChildf(obj[i])+"]");	
					}
					break;
				case "object" :
					Say(kg+"Object["+i+"]：");
					if (obj[i]._____traceFlag_____ != true || obj[i]._____traceFlag_____ ==undefined) {
						Say(" "+kg+"-- 以下["+i+"]内部部件 --");
						Debug.traceAllObj1(obj[i], all, n+1,list);
						list.push(obj[i]);
					}else{
						Say(" "+kg+"：内部部件略["+GetChildf(obj[i])+"]");	
					}
					break;
				default :
					Say(kg+"未知类型 "+typeof (obj[i])+":"+i+" ["+obj[i]+"]");
					break;
				}
				//end switch
			}
		}else{
				Say(" "+kg+"：内部部件略["+GetChildf(obj)+"]");	
		}
		//end for     
		Say(kg+"-- ------- --");
		if (all) {
			//obj.ASSetPropFlags(obj, null, 1, false);
		}
		return list;
	}
	private static function GetChildf(obj:Object):String{
		var l:Number=4;
		var s:String="";
		for(var i in obj){
			if(i=="_____traceFlag_____") continue;
			if(--l<=0) break;
			s+=i+"|";
		}
		s+="...";
		return s;
	}
}
